/*
 * Copyright (C) 2008 ProSyst Software GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.modulefusion.example.wicket.crud.person;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.modulefusion.example.jpa.personprovider.Person;
import org.modulefusion.example.jpa.personprovider.PersonService;

import com.google.inject.Inject;

public class EditPerson extends WebPage<Object> {
	
	@Inject
	private PersonService personService;

	private Person person;
	
	public EditPerson() {
		this(null);
	}

	public EditPerson(Long id) {
		if (id != null)
		this.person = personService.getPerson(id);
		else
			person = new Person();
		
		FeedbackPanel msgs = new FeedbackPanel("msgs");
		add(msgs);
		
		Form<Person> form = new Form<Person>("form") {
			protected void onSubmit() {
				Person saved = personService.savePerson(EditPerson.this.person);
				setResponsePage(new PersonDetail(saved.getId()));
//				setResponsePage(ListPersons.class);
			}
		};
		form.setModel(new CompoundPropertyModel<Person>(person));
		TextField<String> firstname = new TextField<String>("firstname");
		firstname.setRequired(true);
		form.add(firstname);
		TextField<String> lastname = new TextField<String>("lastname");
		lastname.setRequired(true);
		form.add(lastname);
		add(form);

	}

}
